package com.demo.modules.image.extension.impl;

import com.alibaba.cola.extension.Extensions;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversation;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationParam;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationResult;
import com.alibaba.dashscope.common.MultiModalMessage;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.exception.UploadFileException;
import com.demo.common.exception.BizException;
import com.demo.common.exception.CommonErrorEnum;
import com.demo.constants.AiPlatformConstants;
import com.demo.modules.base.BaseAiConfigDto;
import com.demo.modules.image.extension.ImageContentExtPtService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 阿里云百炼平台
 *
 * @author admin
 */
@Extensions(
        bizId = {
                AiPlatformConstants.ALI_YUN
        }
)
public class AliYunDashScopeExe implements ImageContentExtPtService {
    private static final Logger log = LoggerFactory.getLogger(AliYunDashScopeExe.class);

    private static final MultiModalMessage SYSTEM_MESSAGE;

    static {
        Map<String, Object> text = Collections.singletonMap("text", "You are a helpful assistant.");
        List<Map<String, Object>> list = Collections.singletonList(text);
        SYSTEM_MESSAGE = MultiModalMessage.builder().role(Role.SYSTEM.getValue()).content(list).build();
    }

    /**
     * 解析图片内容
     */
    @Override
    public String parseImageContent(String imageUrl, BaseAiConfigDto baseAiConfigDto) {
        try {
            MultiModalConversation conv = new MultiModalConversation();
            // 图片信息
            Map<String, Object> imageMap = Collections.singletonMap("image", imageUrl);
            // 提示词
            Map<String, Object> promptMap = Collections.singletonMap("text", baseAiConfigDto.getPrompt());
            List<Map<String, Object>> content = Arrays.asList(imageMap, promptMap);
            MultiModalMessage userMessage = MultiModalMessage.builder().role(Role.USER.getValue()).content(content).build();

            MultiModalConversationParam param = MultiModalConversationParam.builder()
                    .apiKey(baseAiConfigDto.getAppSecret())
                    .model(baseAiConfigDto.getModel())
                    .messages(Arrays.asList(SYSTEM_MESSAGE, userMessage))
                    .build();
            MultiModalConversationResult result = conv.call(param);
            return (String) result.getOutput().getChoices().get(0).getMessage().getContent().get(0).get("text");
        } catch (NoApiKeyException e) {
            throw new BizException(CommonErrorEnum.NO_API_KEY);
        } catch (UploadFileException e) {
            throw new BizException(CommonErrorEnum.FILE_UPLOAD_FAIL);
        } catch (Exception e) {
            log.error("解析图片内容失败", e);
            throw new BizException(e);
        }
    }
}
